#import "DDRegisterViewController.h"
#import "DDRegisterInput.h"
@interface DDRegisterViewController ()
@property (nonatomic, strong) DDRegisterInput *input;
@property (nonatomic, strong) DDButton *registerBtn;
@property (nonatomic, strong) DDButton *visitorBtn;
@property (nonatomic, strong) DDButton *yj_registerBtn;
@property (nonatomic, strong) DDButton *serviceBtn;
@property (nonatomic, strong) DDButton *loginBtn;
@property (nonatomic, copy) NSString *pass;
@end
@implementation DDRegisterViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (DDRegisterInput *)input
{
    if (!_input)
    {
        _input = [[DDRegisterInput alloc] init];
        [self.ddview addSubview:_input];
        _input.c_left.c_equal(self.ddview).with.coffset(GAP);
        _input.c_right.c_equal(self.ddview).with.coffset(-GAP);
        _input.c_height.c_equal(TEXTFILDHEIGHT * 3);
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            _input.c_bottom.c_equal(self.ddview.c_centerY).with.coffset(- GAP * 2);
        else
            _input.c_top.c_equal(self.ddview.bar.c_bottom).with.coffset(0);
    }
    return _input;
}
- (DDButton *)registerBtn
{
    if (!_registerBtn)
    {
        _registerBtn = [[DDButton alloc] init];
        _registerBtn.title = @"立即注册";
        _registerBtn.backgroundColor = [DDHelper help].color;
        _registerBtn.layer.cornerRadius = GAP / 2.0;
        [_registerBtn addTarget:self action:@selector(dd_actionForRegister) forControlEvents:UIControlEventTouchUpInside];
        [self.ddview addSubview:_registerBtn];
        _registerBtn.c_left.c_equal(self.input);
        _registerBtn.c_right.c_equal(self.input);
        _registerBtn.c_height.c_equal(BUTTONHEIGHT);
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            _registerBtn.c_top.c_equal(self.ddview.c_centerY).with.coffset(GAP * 2);
        else
            _registerBtn.c_top.c_equal(self.input.c_bottom).with.coffset(GAP);
    }
    return _registerBtn;
}
- (DDButton *)visitorBtn
{
    if (!_visitorBtn)
    {
        _visitorBtn = [[DDButton alloc] init];
        _visitorBtn.title = @"游客模式";
        _visitorBtn.backgroundColor = RGBA(60, 123, 68, 1);
        _visitorBtn.layer.cornerRadius = GAP / 2.0;
        [_visitorBtn addTarget:self action:@selector(dd_actionForVisitorBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.ddview addSubview:_visitorBtn];
        _visitorBtn.c_left.c_equal(self.input);
        _visitorBtn.c_right.c_equal(self.input);
        _visitorBtn.c_height.c_equal(BUTTONHEIGHT);
        _visitorBtn.c_top.c_equal(self.registerBtn.c_bottom).with.coffset(GAP);
    }
    return _visitorBtn;
}
- (DDButton *)yj_registerBtn
{
    if (!_yj_registerBtn)
    {
        _yj_registerBtn = [[DDButton alloc] init];
        _yj_registerBtn.title = @"<<一键注册";
        _yj_registerBtn.font = FONT(UIFontWeightRegular, 15);
        [_yj_registerBtn setValue:[DDHelper help].color forKeyPath:@"_label.textColor"];
        _yj_registerBtn.autoTitleSize = YES;
        [_yj_registerBtn addTarget:self action:@selector(dd_actionForYJ_Register) forControlEvents:UIControlEventTouchUpInside];
        [self.ddview addSubview:_yj_registerBtn];
        _yj_registerBtn.c_left.c_equal(self.input);
        if ([[DDHelper help].accountArr count] > 0)
            _yj_registerBtn.c_top.c_equal(self.registerBtn.c_bottom).with.coffset(GAP);
        else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            _yj_registerBtn.c_top.c_equal(self.visitorBtn.c_bottom).with.coffset(GAP);
        else
            _yj_registerBtn.c_bottom.c_equal(self.ddview.c_bottom).with.coffset(-GAP);
    }
    return _yj_registerBtn;
}
- (DDButton *)loginBtn
{
    if (!_loginBtn)
    {
        _loginBtn = [[DDButton alloc] init];
        _loginBtn.title = @"去登录>>";
        _loginBtn.font = FONT(UIFontWeightRegular, 15);
        [_loginBtn setValue:[DDHelper help].color forKeyPath:@"_label.textColor"];
        _loginBtn.autoTitleSize = YES;
        [_loginBtn addTarget:self action:@selector(dd_actionForBackToLogin) forControlEvents:UIControlEventTouchUpInside];
        [self.ddview addSubview:_loginBtn];
        _loginBtn.c_right.c_equal(self.input);
        if ([[DDHelper help].accountArr count] > 0)
            _loginBtn.c_top.c_equal(self.registerBtn.c_bottom).with.coffset(GAP);
        else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            _loginBtn.c_top.c_equal(self.visitorBtn.c_bottom).with.coffset(GAP);
        else
            _loginBtn.c_bottom.c_equal(self.ddview.c_bottom).with.coffset(-GAP);
    }
    return _loginBtn;
}
- (DDButton *)serviceBtn
{
    if (!_serviceBtn)
    {
        _serviceBtn = [[DDButton alloc] init];
        _serviceBtn.font = FONT(UIFontWeightLight, 15);
        [_serviceBtn setValue:[DDHelper help].color forKeyPath:@"_label.textColor"];
        _serviceBtn.autoTitleSize = YES;
        [_serviceBtn addTarget:self action:@selector(dd_actionForServiceAgreement) forControlEvents:UIControlEventTouchUpInside];
        [self.ddview addSubview:_serviceBtn];
        _serviceBtn.c_bottom.c_equal(self.ddview.c_bottom).with.coffset(-GAP);
        _serviceBtn.c_centerX.c_equal(self.ddview);
    }
    return _serviceBtn;
}
- (void)dd_actionForYJ_Register
{
    [CCHud startHud];
    [self.request dd_YJ_registerRequst];
}
- (void)dd_actionForVisitorBtn
{
    [CCHud startHud];
    [self.request dd_visitorRequest];
}
- (void)dd_actionForRegister
{
    if ([self.input.nameTF.text length] < 1) return [CCAlert showMessage:@"请输入账号(6～12位数字或字母)" hidden:nil];
    else if ([self.input.passTF.text length] < 1) return [CCAlert showMessage:@"请输入密码" hidden:nil];
    else if ([self.input.confirmTF.text length] < 1) return [CCAlert showMessage:@"请确认密码" hidden:nil];
    //    else if (![self.input.nameTF.text isMobile]) return [CCAlert showMessage:@"手机号码格式错误" hidden:nil];
    else if ([self.input.nameTF.text length] < 6 || [self.input.nameTF.text length] > 12) return [CCAlert showMessage:@"账号限定6～12位" hidden:nil];
    else if (![self.input.nameTF.text isNumberOrAlphabet]) return [CCAlert showMessage:@"号码仅限于字母和数字" hidden:nil];
    else if (![self.input.passTF.text isNumberOrAlphabet]) return [CCAlert showMessage:@"密码仅限数字和字母的组合" hidden:nil];
    else if (![self.input.passTF.text isEqualToString:self.input.confirmTF.text]) return [CCAlert showMessage:@"两次密码不一致" hidden:nil];
    [CCHud startHud];
    [self.request dd_registerRequst:@{@"name":self.input.nameTF.text,@"password":self.input.passTF.text}];
}
- (void)dd_actionForBackToLogin
{
    if ([NSStringFromClass([[[UIViewController currentVC] presentingViewController] class]) isEqualToString:@"DDLoginViewController"])
        [self dismissViewControllerAnimated:YES completion:nil];
    else [self presentViewController:[NSClassFromString(@"DDLoginViewController") new] animated:NO completion:nil];
}
- (void)dd_actionForServiceAgreement
{
}
#pragma mark - JTWRequestDelegate
- (void)dd_didRequestWithReceiveData:(NSDictionary *)dict error:(NSError *)error
{
    [CCHud stopHud];
    if(error)
    {
        if ((error.code != -999 || ![error.localizedDescription isEqualToString:@"cancelled"]))
            [CCAlert showMessage:[NSString stringWithFormat:@"%@",error.localizedDescription] hidden:nil];
        return;
    }
    if (!dict)
        return;
    int code = -1;
    code = [[dict valueForKey:@"code"] intValue];
    NSString *info = [NSString stringWithFormat:@"%@",[dict valueForKey:@"info"]];
    if (code == 0)
    {
        if (self.request.type == JTWRequestTypeRegister)
        {
            [DDHelper dd_insertAccountDict:@{@"name":self.input.nameTF.text,@"password":self.input.passTF.text}];
            [CCAlert showMessage:(([info length] > 1)?info:@"注册成功") hidden:^{
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
        }
        else if (self.request.type == JTWRequestTypeYJ_Register || self.request.type == JTWRequestTypeVisitor)
        {
            NSString *account = [NSString stringWithFormat:@"%@",[dict valueForKey:@"account"]];
            NSString *accountid = [NSString stringWithFormat:@"%@",[dict valueForKey:@"accountid"]];
            self.pass = [NSString stringWithFormat:@"%@",[dict valueForKey:@"pass"]];
            NSDictionary *loginDict = @{@"name":account,@"password":self.pass};
            [DDHelper dd_insertAccountDict:loginDict];
            [[DDHelper help] setValue:accountid forKey:@"accountID"];
            if (self.request.type == JTWRequestTypeYJ_Register)
            {
                [CCAlert showMessage:[NSString stringWithFormat:@"恭喜,您已注册成功!\n账号:%@\n密码:%@\n为了您的账号安全，请及时修改密码",account,self.pass] title:nil leftBtnText:@"稍后再说" rightBtnText:@"修改密码" leftblock:^{
                    [self dismissViewControllerAnimated:YES completion:nil];
                } rightBlock:^{
                    [self presentViewController:[NSClassFromString(@"DDPassViewController") new] animated:YES completion:nil];
                }];
            }
            else
            {
                [CCHud startHud];
                [self.request dd_loginRequest:loginDict];
            }
        }
        else if (self.request.type == JTWRequestTypeLogin)
        {
            NSString *phone = [dict valueForKey:@"phone"];
            [[DDHelper help] setValue:self.input.nameTF.text forKey:@"account"];
            [[DDHelper help] setValue:[dict valueForKey:@"accountid"] forKey:@"accountID"];
            [[DDHelper help] setValue:[dict valueForKey:@"accesstoken"] forKey:@"token"];
            [[DDHelper help] setValue:[dict valueForKey:@"gameaccountid"] forKeyPath:@"gameAccountID"];
            [[DDHelper help] setValue:[phone length] > 1?phone:@"" forKeyPath:@"phone"];
            [CCAlert showMessage:(([info length] > 1)?info:@"登录成功") hidden:^{
                self.view.hidden = YES;
                UIViewController *vc = self.presentingViewController;
                while (vc.presentingViewController)
                {
                    vc = vc.presentingViewController;
                }
                [vc dismissViewControllerAnimated:NO completion:^{
                    [CCNotice post:DDPlatformLoginNotification obj:nil info:@{@"token":[DDHelper help].token,@"accountID":[DDHelper help].accountID,@"link":[DDHelper help].link}];
                    [DDHelper help].userBtn.hidden = ([[DDHelper help].state intValue] == 1)?NO:YES;
                    
                }];
            }];
        }
    }
    else
    {
        [CCAlert showMessage:[NSString stringWithFormat:@"%@" ,info] title:@"错误" enter:nil];
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.input.nameTF.placeholder = @"请输入账号(6～12位数字或字母)";
    self.input.passTF.placeholder = @"请输入密码(6~16位数字和字母)";
    self.input.confirmTF.placeholder = @"请再次确认密码";
    self.input.nameTF.type = JTWTextFieldTypeAccountNormal;
    [self.input setupline];
    self.registerBtn.hidden = NO;
    self.visitorBtn.hidden = [[DDHelper help].accountArr count] > 0?YES:NO;
    self.yj_registerBtn.hidden = NO;
    self.loginBtn.hidden = NO;
    self.serviceBtn.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
